package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.VersionTypeCountDto;
import cn.com.cifi.mars.bean.vo.price.*;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.*;
import cn.com.cifi.mars.mapper.mars.*;
import cn.com.cifi.mars.service.*;
import cn.com.cifi.mars.util.StrUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 货值审批全盘差异概览表 服务实现类
 * </p>
 *
 * @author lyb
 * @since 2021-08-10
 */
@Service
public class PriceApprovalDifferencesServiceImpl extends ServiceImpl<PriceApprovalDifferencesMapper, PriceApprovalDifferences> implements PriceApprovalDifferencesService {

    @Autowired
    private ApprovalBillMapper approvalBillMapper;
    @Autowired
    private PriceApprovalDifferencesMapper priceApprovalDifferencesMapper;
    @Autowired
    private ProjectBasicService projectBasicService;
    @Autowired
    private SubmitApprovedProjectService submitApprovedProjectService;
    @Autowired
    private PriceVersionService priceVersionService;
    @Autowired
    private ApprovalVersionCompareService approvalVersionCompareService;
    @Autowired
    private StageService stageService;

    @Override
    public PriceApprovalDifferencesVo getByBusinessKey(String businessKey, String type) {

        //调整前数据概览
        PriceApprovalDifferences beforeContent
                = priceApprovalDifferencesMapper.selectOne(new QueryWrapper<PriceApprovalDifferences>().eq("bill_id", businessKey).eq("vs_type", type).eq("data_type", "调整前"));
        unitConversion(beforeContent);
        //调整后数据概览
        PriceApprovalDifferences afterContent
                = priceApprovalDifferencesMapper.selectOne(new QueryWrapper<PriceApprovalDifferences>().eq("bill_id", businessKey).eq("vs_type", type).eq("data_type", "调整后"));
        unitConversion(afterContent);
        //差异数据概览
        PriceApprovalDifferences differenceValueList
                = priceApprovalDifferencesMapper.selectOne(new QueryWrapper<PriceApprovalDifferences>().eq("bill_id", businessKey).eq("vs_type", type).eq("data_type", "差异数"));
        unitConversion(differenceValueList);
        //差异比例概览
        PriceApprovalDifferences differenceRateList
                = priceApprovalDifferencesMapper.selectOne(new QueryWrapper<PriceApprovalDifferences>().eq("bill_id", businessKey).eq("vs_type", type).eq("data_type", "差异比"));
        //转化为百分比
        if (differenceRateList != null) {
            DecimalFormat df = new DecimalFormat("0.00%");
            differenceRateList.setTotalValue(df.format(new BigDecimal(differenceRateList.getTotalValue())));
            differenceRateList.setQuityValue(df.format(new BigDecimal(differenceRateList.getQuityValue())));
            differenceRateList.setHouseValue(df.format(new BigDecimal(differenceRateList.getHouseValue())));
            differenceRateList.setHouseAvg(df.format(new BigDecimal(differenceRateList.getHouseAvg())));
            differenceRateList.setHouseArea(df.format(new BigDecimal(differenceRateList.getHouseArea())));
            differenceRateList.setHouseNum(df.format(new BigDecimal(differenceRateList.getHouseNum())));
            differenceRateList.setParkingValue(df.format(new BigDecimal(differenceRateList.getParkingValue())));
            differenceRateList.setParkingAvg(df.format(new BigDecimal(differenceRateList.getParkingAvg())));
            differenceRateList.setParkingNum(df.format(new BigDecimal(differenceRateList.getParkingNum())));
            differenceRateList.setForsaleValue(df.format(new BigDecimal(differenceRateList.getForsaleValue())));
            differenceRateList.setForsaleNum(df.format(new BigDecimal(differenceRateList.getForsaleNum())));
        }


        PriceApprovalDifferencesVo priceApprovalDifferencesVo = new PriceApprovalDifferencesVo();
        priceApprovalDifferencesVo.setBeforeContent(beforeContent);
        priceApprovalDifferencesVo.setAfterContent(afterContent);
        priceApprovalDifferencesVo.setDifferenceValueList(differenceValueList);
        priceApprovalDifferencesVo.setDifferenceRateList(differenceRateList);

        return priceApprovalDifferencesVo;
    }

    private void unitConversion(PriceApprovalDifferences cotent) {
        if (cotent != null) {
            //单位改为万元
            cotent.setTotalValue(String.valueOf(Math.round(Double.parseDouble(cotent.getTotalValue()) / 10000)));
            cotent.setQuityValue(String.valueOf(Math.round(Double.parseDouble(cotent.getQuityValue()) / 10000)));
            cotent.setHouseValue(String.valueOf(Math.round(Double.parseDouble(cotent.getHouseValue()) / 10000)));
            cotent.setParkingValue(String.valueOf(Math.round(Double.parseDouble(cotent.getParkingValue()) / 10000)));
            cotent.setForsaleValue(String.valueOf(Math.round(Double.parseDouble(cotent.getForsaleValue()) / 10000)));

            //取整
            cotent.setHouseAvg(String.valueOf(Math.round(Double.parseDouble(cotent.getHouseAvg()))));
            cotent.setHouseArea(String.format("%.2f", Double.parseDouble(cotent.getHouseArea())));
            cotent.setParkingAvg(String.valueOf(Math.round(Double.parseDouble(cotent.getParkingAvg()))));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveData(String id) {
        List<DifferenceReviewVersionVO> differenceReview = getDifferenceReview(null, id);
        ApprovalBill approvalBill = approvalBillMapper.selectById(id);


        differenceReview.forEach(vo -> {
            PriceApprovalDifferences priceApprovalDifferences = new PriceApprovalDifferences();
            //调整前
            setCoreDetail(id, approvalBill, vo, priceApprovalDifferences, "调整前");
            priceApprovalDifferences.setTotalValue(vo.getContentVO().getTotalValue().toString());
            priceApprovalDifferences.setQuityValue(vo.getContentVO().getQuityValue().toString());
            priceApprovalDifferences.setHouseValue(vo.getContentVO().getHouseCommodityValueCount().toString());
            priceApprovalDifferences.setHouseAvg(vo.getContentVO().getHouseAvg().toString());
            priceApprovalDifferences.setHouseArea(vo.getContentVO().getSaleAreaCount().toString());
            priceApprovalDifferences.setHouseNum(String.valueOf(vo.getContentVO().getRoomNumCount()));
            priceApprovalDifferences.setParkingValue(vo.getContentVO().getParkingValueCount().toString());
            priceApprovalDifferences.setParkingAvg(vo.getContentVO().getParkingAvg().toString());
            priceApprovalDifferences.setParkingNum(String.valueOf(vo.getContentVO().getParkingNumCount()));
            priceApprovalDifferences.setForsaleValue(vo.getContentVO().getForSaleParkingValueCount().toString());
            priceApprovalDifferences.setForsaleNum(String.valueOf(vo.getContentVO().getForSaleParkingNumCount()));
            priceApprovalDifferences.setCreateTime(LocalDateTime.now());
            priceApprovalDifferencesMapper.insert(priceApprovalDifferences);

            //调整后
            setCoreDetail(id, approvalBill, vo, priceApprovalDifferences, "调整后");
            priceApprovalDifferences.setTotalValue(vo.getAfterContentVO().getTotalValue().toString());
            priceApprovalDifferences.setQuityValue(vo.getAfterContentVO().getQuityValue().toString());
            priceApprovalDifferences.setHouseValue(vo.getAfterContentVO().getHouseCommodityValueCount().toString());
            priceApprovalDifferences.setHouseAvg(vo.getAfterContentVO().getHouseAvg().toString());
            priceApprovalDifferences.setHouseArea(vo.getAfterContentVO().getSaleAreaCount().toString());
            priceApprovalDifferences.setHouseNum(String.valueOf(vo.getAfterContentVO().getRoomNumCount()));
            priceApprovalDifferences.setParkingValue(vo.getAfterContentVO().getParkingValueCount().toString());
            priceApprovalDifferences.setParkingAvg(vo.getAfterContentVO().getParkingAvg().toString());
            priceApprovalDifferences.setParkingNum(String.valueOf(vo.getAfterContentVO().getParkingNumCount()));
            priceApprovalDifferences.setForsaleValue(vo.getAfterContentVO().getForSaleParkingValueCount().toString());
            priceApprovalDifferences.setForsaleNum(String.valueOf(vo.getAfterContentVO().getForSaleParkingNumCount()));
            priceApprovalDifferences.setCreateTime(LocalDateTime.now());
            priceApprovalDifferencesMapper.insert(priceApprovalDifferences);

            //差异数
            setCoreDetail(id, approvalBill, vo, priceApprovalDifferences, "差异数");
            priceApprovalDifferences.setTotalValue(vo.getDifferenceVO().getTotalValueDiff().toString());
            priceApprovalDifferences.setQuityValue(vo.getDifferenceVO().getQuityValueDiff().toString());
            priceApprovalDifferences.setHouseValue(vo.getDifferenceVO().getHouseCommodityValueCountDiff().toString());
            priceApprovalDifferences.setHouseAvg(vo.getDifferenceVO().getHouseAvgDiff().toString());
            priceApprovalDifferences.setHouseArea(vo.getDifferenceVO().getSaleAreaCountDiff().toString());
            priceApprovalDifferences.setHouseNum(String.valueOf(vo.getDifferenceVO().getRoomNumCountDiff()));
            priceApprovalDifferences.setParkingValue(vo.getDifferenceVO().getParkingValueCountDiff().toString());
            priceApprovalDifferences.setParkingAvg(vo.getDifferenceVO().getParkingAvgDiff().toString());
            priceApprovalDifferences.setParkingNum(vo.getDifferenceVO().getParkingNumCountDiff().toString());
            priceApprovalDifferences.setForsaleValue(vo.getDifferenceVO().getForSaleParkingValueCountDiff().toString());
            priceApprovalDifferences.setForsaleNum(vo.getDifferenceVO().getForSaleParkingNumCountDiff().toString());
            priceApprovalDifferences.setCreateTime(LocalDateTime.now());
            priceApprovalDifferencesMapper.insert(priceApprovalDifferences);

            //差异比
            setCoreDetail(id, approvalBill, vo, priceApprovalDifferences, "差异比");
            priceApprovalDifferences.setTotalValue(vo.getDifferenceVO().getTotalValuePer().toString());
            priceApprovalDifferences.setQuityValue(vo.getDifferenceVO().getQuityValuePer().toString());
            priceApprovalDifferences.setHouseValue(vo.getDifferenceVO().getHouseCommodityValueCountPer().toString());
            priceApprovalDifferences.setHouseAvg(vo.getDifferenceVO().getHouseAvgPer().toString());
            priceApprovalDifferences.setHouseArea(vo.getDifferenceVO().getSaleAreaCountPer().toString());
            priceApprovalDifferences.setHouseNum(String.valueOf(vo.getDifferenceVO().getRoomNumCountPer()));
            priceApprovalDifferences.setParkingValue(vo.getDifferenceVO().getParkingValueCountPer().toString());
            priceApprovalDifferences.setParkingAvg(vo.getDifferenceVO().getParkingAvgPer().toString());
            priceApprovalDifferences.setParkingNum(String.valueOf(vo.getDifferenceVO().getParkingNumCountPer()));
            priceApprovalDifferences.setForsaleValue(vo.getDifferenceVO().getForSaleParkingValueCountPer().toString());
            priceApprovalDifferences.setForsaleNum(String.valueOf(vo.getDifferenceVO().getForSaleParkingNumCountPer()));
            priceApprovalDifferences.setCreateTime(LocalDateTime.now());
            priceApprovalDifferencesMapper.insert(priceApprovalDifferences);


        });


    }

    @Override
    public boolean selectOne(String businessKey) {
        List<PriceApprovalDifferences> priceApprovalDifferences = priceApprovalDifferencesMapper.selectList(new QueryWrapper<PriceApprovalDifferences>().eq("bill_id",businessKey));
        if (priceApprovalDifferences.isEmpty()){
            return false;
        }
        return true;
    }

    private void setCoreDetail(String id, ApprovalBill approvalBill, DifferenceReviewVersionVO vo, PriceApprovalDifferences priceApprovalDifferences, String dataType) {
        priceApprovalDifferences.setId(UUIDUtils.create());
        priceApprovalDifferences.setProjectId(approvalBill.getProjectId());
        priceApprovalDifferences.setBillId(id);
        priceApprovalDifferences.setState(approvalBill.getState());
        priceApprovalDifferences.setVsType(vo.getVersionName());
        priceApprovalDifferences.setDataType(dataType);
    }

    private List<DifferenceReviewVersionVO> getDifferenceReview(String projectId, String businessKey) {
        List<DifferenceReviewVersionVO> resultList = new ArrayList<>();
        if (StringUtils.isNotBlank(businessKey)) {
            ApprovalBill approvalBill = submitApprovedProjectService.selectApprovalBillByKey(businessKey);
            if (approvalBill != null) {
                projectId = approvalBill.getProjectId();
                if (approvalBill.getState().equals(ApprovedStateCodeEnum.APPROVAL_SUCCESS.getKey()) ||
                        approvalBill.getState().equals(ApprovedStateCodeEnum.APPROVAL.getKey())) {
                    List<ApprovalVersionCompare> approvalVersionCompares = approvalVersionCompareService.selectByBillId(businessKey);

                    //查询权益比例
                    MdmProjectPatner projectPatner = projectBasicService.selecteQuityRatio(projectId);
                    ValueReviewVersionContentVO castMaps = prepareCompareValue(CollectTypeEnum.CASTDECISION.getKey(), approvalVersionCompares, projectPatner);
                    ValueReviewVersionContentVO warPlanMaps = prepareCompareValue(CollectTypeEnum.WARPLAN.getKey(), approvalVersionCompares, projectPatner);
                    ValueReviewVersionContentVO getPriceMaps = prepareCompareValue(CollectTypeEnum.GETPRICE.getKey(), approvalVersionCompares, projectPatner);
                    ValueReviewVersionContentVO adjustAfterMaps = prepareCompareValue(CollectTypeEnum.ADJUST_AFTER.getKey(), approvalVersionCompares, projectPatner);
                    ValueReviewVersionContentVO adjustBeforeMaps = prepareCompareValue(CollectTypeEnum.ADJUST_BEFORE.getKey(), approvalVersionCompares, projectPatner);

                    //调整后 VS 投资版对比
                    if (null != castMaps) {
                        DifferenceReviewVO differenceReviewVO = newCalDifference(adjustAfterMaps, castMaps);
                        DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                        reviewVersionVO.setVersionName(CollectTypeEnum.CASTDECISION.getValue());
                        reviewVersionVO.setAfterContentVO(adjustAfterMaps);
                        reviewVersionVO.setContentVO(castMaps);
                        reviewVersionVO.setDifferenceVO(differenceReviewVO);
                        resultList.add(reviewVersionVO);
                    }
                    //调整后  VS 战规版对比
                    if (null != warPlanMaps) {
                        DifferenceReviewVO differenceReviewVO = newCalDifference(adjustAfterMaps, warPlanMaps);
                        DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                        reviewVersionVO.setVersionName(CollectTypeEnum.WARPLAN.getValue());
                        reviewVersionVO.setAfterContentVO(adjustAfterMaps);
                        reviewVersionVO.setContentVO(warPlanMaps);
                        reviewVersionVO.setDifferenceVO(differenceReviewVO);
                        resultList.add(reviewVersionVO);
                    }
                    //调整后  VS 大定价版对比
                    if (null != getPriceMaps) {
                        DifferenceReviewVO differenceReviewVO = newCalDifference(adjustAfterMaps, getPriceMaps);
                        DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                        reviewVersionVO.setVersionName(CollectTypeEnum.GETPRICE.getValue());
                        reviewVersionVO.setAfterContentVO(adjustAfterMaps);
                        reviewVersionVO.setContentVO(getPriceMaps);
                        reviewVersionVO.setDifferenceVO(differenceReviewVO);
                        resultList.add(reviewVersionVO);
                    }
                    //调整后  VS 调整前对比
                    if (null != adjustBeforeMaps) {
                        DifferenceReviewVO differenceReviewVO = newCalDifference(adjustAfterMaps, adjustBeforeMaps);
                        DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                        reviewVersionVO.setVersionName(CollectTypeEnum.ADJUST_BEFORE.getValue());
                        reviewVersionVO.setAfterContentVO(adjustAfterMaps);
                        reviewVersionVO.setContentVO(adjustBeforeMaps);
                        reviewVersionVO.setDifferenceVO(differenceReviewVO);
                        resultList.add(reviewVersionVO);
                    }
                    return resultList;
                }
            }
        }

        if (StrUtils.isNotEmpty(projectId)) {
            List<ValueReviewVersionVO> reviewVersionVOList = prepareVsData(projectId);
            DifferenceReviewVO beforeDiffVO = null;
            DifferenceReviewVO castDiffVO = null;
            DifferenceReviewVO warDiffVO = null;
            DifferenceReviewVO priceDiffVO = null;
            ValueReviewVersionContentVO afterContentVO = null;
            ValueReviewVersionContentVO beforeContentVO = null;
            ValueReviewVersionContentVO castContentVO = null;
            ValueReviewVersionContentVO warContentVO = null;
            ValueReviewVersionContentVO priceContentVO = null;
            //封装返回结果
            for (ValueReviewVersionVO versionVO : reviewVersionVOList) {
                if (versionVO.getVersionName().equals(CollectTypeEnum.ADJUST_AFTER.getValue())) {
                    afterContentVO = versionVO.getContentVO();
                }
            }
            for (ValueReviewVersionVO versionVO : reviewVersionVOList) {
                if (versionVO.getVersionName().equals(CollectTypeEnum.CASTDECISION.getValue())) {
                    DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                    castContentVO = versionVO.getContentVO();
                    castDiffVO = newCalDifference(afterContentVO, castContentVO);
                    reviewVersionVO.setVersionName(CollectTypeEnum.CASTDECISION.getValue());
                    reviewVersionVO.setAfterContentVO(afterContentVO);
                    reviewVersionVO.setContentVO(castContentVO);
                    reviewVersionVO.setDifferenceVO(castDiffVO);
                    resultList.add(reviewVersionVO);
                } else if (versionVO.getVersionName().equals(CollectTypeEnum.WARPLAN.getValue())) {
                    DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                    warContentVO = versionVO.getContentVO();
                    warDiffVO = newCalDifference(afterContentVO, warContentVO);
                    reviewVersionVO.setVersionName(CollectTypeEnum.WARPLAN.getValue());
                    reviewVersionVO.setAfterContentVO(afterContentVO);
                    reviewVersionVO.setContentVO(warContentVO);
                    reviewVersionVO.setDifferenceVO(warDiffVO);
                    resultList.add(reviewVersionVO);
                } else if (versionVO.getVersionName().equals(CollectTypeEnum.GETPRICE.getValue())) {
                    DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                    priceContentVO = versionVO.getContentVO();
                    priceDiffVO = newCalDifference(afterContentVO, priceContentVO);
                    reviewVersionVO.setVersionName(CollectTypeEnum.GETPRICE.getValue());
                    reviewVersionVO.setAfterContentVO(afterContentVO);
                    reviewVersionVO.setContentVO(priceContentVO);
                    reviewVersionVO.setDifferenceVO(priceDiffVO);
                    resultList.add(reviewVersionVO);
                } else if (versionVO.getVersionName().equals(CollectTypeEnum.ADJUST_BEFORE.getValue())) {
                    DifferenceReviewVersionVO reviewVersionVO = new DifferenceReviewVersionVO();
                    beforeContentVO = versionVO.getContentVO();
                    beforeDiffVO = newCalDifference(afterContentVO, beforeContentVO);
                    reviewVersionVO.setVersionName(CollectTypeEnum.ADJUST_BEFORE.getValue());
                    reviewVersionVO.setAfterContentVO(afterContentVO);
                    reviewVersionVO.setContentVO(beforeContentVO);
                    reviewVersionVO.setDifferenceVO(beforeDiffVO);
                    resultList.add(reviewVersionVO);
                }
            }
            return resultList;
        }

        return null;
    }

    private ValueReviewVersionContentVO prepareCompareValue(String collectType, List<ApprovalVersionCompare> approvalVersionCompares, MdmProjectPatner projectPatner) {
        //详情对象（用于封装数据）
        ValueReviewVersionContentVO content = new ValueReviewVersionContentVO();
        //计算返回的数值
        //总房源数
        int roomNumCount = 0;
        //可售车位数
        int parkingNumCount = 0;
        //未推售车位数
        int forSaleParkingNumCount = 0;
        //房源货值
        BigDecimal houseCommodityValueCount = BigDecimal.ZERO;
        //可售车位货值
        BigDecimal parkingValueCount = BigDecimal.ZERO;
        //未推售车位货值
        BigDecimal forSaleParkingValueCount = BigDecimal.ZERO;
        //总面积
        BigDecimal saleAreaCount = BigDecimal.ZERO;
        //总货值
        BigDecimal totalValue = BigDecimal.ZERO;
        //权益货值
        BigDecimal quityValue = BigDecimal.ZERO;
        //房源均价
        BigDecimal houseAvg = BigDecimal.ZERO;
        //车位均价
        BigDecimal parkingAvg = BigDecimal.ZERO;

        List<String> versionCompares = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(approvalVersionCompares)) {
            for (ApprovalVersionCompare obj : approvalVersionCompares) {
                String stageStatus = obj.getStageStatus();
                if (obj.getCollectType().equals(collectType)) {
                    if (collectType.equals(CollectTypeEnum.ADJUST_AFTER.getKey()) || collectType.equals(CollectTypeEnum.ADJUST_BEFORE.getKey())) {
                        if (StringUtils.isEmpty(stageStatus) || stageStatus.equals(StageStatusEnum.CTIVE.getKey())) {
                            if (obj.getCollectType().equals(collectType)) {
                                versionCompares.add(obj.getVersionIdCompare());
                            }
                        }
                    } else {
                        if (obj.getCollectType().equals(collectType)) {
                            versionCompares.add(obj.getVersionIdCompare());
                        }
                    }
                }
            }
        }

        if (CollectionUtils.isNotEmpty(versionCompares)) {
            for (String versionId : versionCompares) {
                PriceValueOrg priceValueOrg = priceVersionService.selectViewDataById(versionId);
                if (priceValueOrg != null) {
                    PriceVersion version = priceVersionService.selectById(versionId);
                    String valueString = priceVersionService.selectArea(versionId, version.getVersionType());

                    Integer roomNum = priceValueOrg.getRoomNum() == null ? 0 : priceValueOrg.getRoomNum();
                    roomNumCount = roomNumCount + roomNum;

                    Integer parkingNum = priceValueOrg.getParkingNum() == null ? 0 : priceValueOrg.getParkingNum();
                    parkingNumCount = parkingNumCount + parkingNum;

                    Integer forsaleParkingNum = priceValueOrg.getForsaleParkingNum() == null ? 0 : priceValueOrg.getForsaleParkingNum();
                    forSaleParkingNumCount = forSaleParkingNumCount + forsaleParkingNum;

                    BigDecimal houseCommodityValue = priceValueOrg.getHouseCommodityValue() == null ? BigDecimal.ZERO : priceValueOrg.getHouseCommodityValue();
                    houseCommodityValueCount = houseCommodityValueCount.add(houseCommodityValue);

                    BigDecimal notCommodityValue = priceValueOrg.getNotCommodityValue() == null ? BigDecimal.ZERO : priceValueOrg.getNotCommodityValue();
                    parkingValueCount = parkingValueCount.add(notCommodityValue);

                    BigDecimal commodityValue = priceValueOrg.getCommodityValue() == null ? BigDecimal.ZERO : priceValueOrg.getCommodityValue();
                    forSaleParkingValueCount = forSaleParkingValueCount.add(commodityValue);

                    BigDecimal saleArea = StrUtils.isEmpty(valueString) ? BigDecimal.ZERO : new BigDecimal(valueString);
                    saleAreaCount = saleAreaCount.add(saleArea);
                }
            }
            totalValue = houseCommodityValueCount.add(parkingValueCount);
            //查询权益比例
            if (null != projectPatner.getEquityRatio()) {
                //计算权益货值
                BigDecimal divide = projectPatner.getEquityRatio().divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);
                quityValue = totalValue.multiply(divide);
            }
            if (null != saleAreaCount && saleAreaCount.compareTo(BigDecimal.ZERO) != 0) {
                houseAvg = houseCommodityValueCount.divide(saleAreaCount, 4, BigDecimal.ROUND_HALF_UP);
            }
            BigDecimal parkingNumCountDec = new BigDecimal(parkingNumCount);
            if (null != parkingNumCountDec && parkingNumCountDec.compareTo(BigDecimal.ZERO) != 0) {
                parkingAvg = parkingValueCount.divide(parkingNumCountDec, 4, BigDecimal.ROUND_HALF_UP);
            }
        } else if (collectType.equals(CollectTypeEnum.ADJUST_BEFORE.getKey()) && CollectionUtils.isEmpty(versionCompares)) {
            content.setRoomNumCount(roomNumCount);
            content.setParkingNumCount(parkingNumCount);
            content.setForSaleParkingNumCount(forSaleParkingNumCount);
            content.setHouseCommodityValueCount(houseCommodityValueCount);
            content.setParkingValueCount(parkingValueCount);
            content.setForSaleParkingValueCount(forSaleParkingValueCount);
            content.setSaleAreaCount(saleAreaCount);
            content.setTotalValue(totalValue);
            content.setQuityValue(quityValue);
            content.setHouseAvg(houseAvg);
            content.setParkingAvg(parkingAvg);
            return content;
        } else {
            return null;
        }
        content.setRoomNumCount(roomNumCount);
        content.setParkingNumCount(parkingNumCount);
        content.setForSaleParkingNumCount(forSaleParkingNumCount);
        content.setHouseCommodityValueCount(houseCommodityValueCount);
        content.setParkingValueCount(parkingValueCount);
        content.setForSaleParkingValueCount(forSaleParkingValueCount);
        content.setSaleAreaCount(saleAreaCount);
        content.setTotalValue(totalValue);
        content.setQuityValue(quityValue);
        content.setHouseAvg(houseAvg);
        content.setParkingAvg(parkingAvg);
        return content;
    }

    public DifferenceReviewVO newCalDifference(ValueReviewVersionContentVO adjustAfterVO, ValueReviewVersionContentVO vo) {
        DifferenceReviewVO diffVo = new DifferenceReviewVO();
        if (adjustAfterVO == null) {
            adjustAfterVO = new ValueReviewVersionContentVO();
        }
        if (vo == null) {
            vo = new ValueReviewVersionContentVO();
        }
        //总货值
        BigDecimal totalValueDiff = adjustAfterVO.getTotalValue().subtract(vo.getTotalValue());
        BigDecimal totalValuePer = parpareDiffPer(vo.getTotalValue(), adjustAfterVO.getTotalValue());
        diffVo.setTotalValueDiff(totalValueDiff);
        diffVo.setTotalValuePer(totalValuePer);
        //权益货值
        BigDecimal quityValueDiff = adjustAfterVO.getQuityValue().subtract(vo.getQuityValue());
        BigDecimal quityValuePer = parpareDiffPer(vo.getQuityValue(), adjustAfterVO.getQuityValue());
        diffVo.setQuityValueDiff(quityValueDiff);
        diffVo.setQuityValuePer(quityValuePer);
        //房源货值
        BigDecimal houseCommodityValueCountDiff = adjustAfterVO.getHouseCommodityValueCount().subtract(vo.getHouseCommodityValueCount());
        BigDecimal houseCommodityValueCountPer = parpareDiffPer(vo.getHouseCommodityValueCount(), adjustAfterVO.getHouseCommodityValueCount());
        diffVo.setHouseCommodityValueCountDiff(houseCommodityValueCountDiff);
        diffVo.setHouseCommodityValueCountPer(houseCommodityValueCountPer);
        //房源均价
        BigDecimal houseAvgDiff = adjustAfterVO.getHouseAvg().subtract(vo.getHouseAvg());
        BigDecimal houseAvgPer = parpareDiffPer(vo.getHouseAvg(), adjustAfterVO.getHouseAvg());
        diffVo.setHouseAvgDiff(houseAvgDiff);
        diffVo.setHouseAvgPer(houseAvgPer);
        //房源面积
        BigDecimal saleAreaCountDiff = adjustAfterVO.getSaleAreaCount().subtract(vo.getSaleAreaCount());
        BigDecimal saleAreaCountPer = parpareDiffPer(vo.getSaleAreaCount(), adjustAfterVO.getSaleAreaCount());
        diffVo.setSaleAreaCountDiff(saleAreaCountDiff);
        diffVo.setSaleAreaCountPer(saleAreaCountPer);
        //总房源
        int roomNumCountDiff = adjustAfterVO.getRoomNumCount() - vo.getRoomNumCount();
        BigDecimal roomNumCountPer = parpareDiffPer(new BigDecimal(vo.getRoomNumCount()), new BigDecimal(adjustAfterVO.getRoomNumCount()));
        diffVo.setRoomNumCountDiff(roomNumCountDiff);
        diffVo.setRoomNumCountPer(roomNumCountPer);
        //可售车位货值
        BigDecimal parkingValueCountDiff = adjustAfterVO.getParkingValueCount().subtract(vo.getParkingValueCount());
        BigDecimal parkingValueCountPer = parpareDiffPer(vo.getParkingValueCount(), adjustAfterVO.getParkingValueCount());
        diffVo.setParkingValueCountDiff(parkingValueCountDiff);
        diffVo.setParkingValueCountPer(parkingValueCountPer);
        //可售车位均价
        BigDecimal parkingAvgDiff = adjustAfterVO.getParkingAvg().subtract(vo.getParkingAvg());
        BigDecimal parkingAvgPer = parpareDiffPer(vo.getParkingAvg(), adjustAfterVO.getParkingAvg());
        diffVo.setParkingAvgDiff(parkingAvgDiff);
        diffVo.setParkingAvgPer(parkingAvgPer);
        //可售车位数
        int parkingNumCountDiff = adjustAfterVO.getParkingNumCount() - vo.getParkingNumCount();
        BigDecimal parkingNumCountPer = parpareDiffPer(new BigDecimal(vo.getParkingNumCount()), new BigDecimal(adjustAfterVO.getParkingNumCount()));
        diffVo.setParkingNumCountDiff(parkingNumCountDiff);
        diffVo.setParkingNumCountPer(parkingNumCountPer);
        //未推售车位货值
        BigDecimal forSaleParkingValueCountDiff = adjustAfterVO.getForSaleParkingValueCount().subtract(vo.getForSaleParkingValueCount());
        BigDecimal forSaleParkingValueCountPer = parpareDiffPer(vo.getForSaleParkingValueCount(), adjustAfterVO.getForSaleParkingValueCount());
        diffVo.setForSaleParkingValueCountDiff(forSaleParkingValueCountDiff);
        diffVo.setForSaleParkingValueCountPer(forSaleParkingValueCountPer);
        //未推售车位数
        int forSaleParkingNumCountDiff = adjustAfterVO.getForSaleParkingNumCount() - vo.getForSaleParkingNumCount();
        BigDecimal forSaleparkingNumCountPer = parpareDiffPer(new BigDecimal(vo.getForSaleParkingNumCount()), new BigDecimal(adjustAfterVO.getForSaleParkingNumCount()));
        diffVo.setForSaleParkingNumCountDiff(forSaleParkingNumCountDiff);
        diffVo.setForSaleParkingNumCountPer(forSaleparkingNumCountPer);
        return diffVo;
    }

    private List<ValueReviewVersionVO> prepareVsData(String projectId) {
        List<ValueReviewVersionVO> resultList = new ArrayList<>();
        //根据projectId查询需要审批的项目的分期
        List<PriceVersion> priceVersionList = submitApprovedProjectService.selectApprovedProject(projectId);
        //根据projectId查询项目下所有分期的所有版本的个数
        List<VersionTypeCountDto> versionTypeCountDtoList = submitApprovedProjectService.selectVersionTypeCount(projectId);
        //根据projectId查询项目下所有分期个数
        List<MdmStage> approvedStageDtoList = stageService.selectByProjectId(projectId, IsDeleteEnum.NO.getKey());

        //查询权益比例
        MdmProjectPatner projectPatner = projectBasicService.selecteQuityRatio(projectId);
        //计算项目下所有分期的个数
        int stageCount = 0;
        List<String> projectfIds = new ArrayList<>();
        if (null != approvedStageDtoList) {
            stageCount = approvedStageDtoList.size();
            for (MdmStage projectByStages : approvedStageDtoList) {
                projectfIds.add(projectByStages.getProjectFid());
            }
        }
        List<String> approvedVersions = new ArrayList<String>();
        if (CollectionUtils.isNotEmpty(priceVersionList)) {
            for (PriceVersion priceVersion : priceVersionList) {
                approvedVersions.add(priceVersion.getVersionType());
            }
        }
        //遍历存有审批版本的集合 approvedVersion
        int castDecisionCount = 0;
        int strategyCount = 0;
        int getPriceCount = 0;
        int dynamicCount = 0;
        for (String versionType : approvedVersions) {
            if (VersionTypeEunm.INVESTMENT.getKey().equals(versionType)) {
                castDecisionCount++;
            } else if (VersionTypeEunm.WARPLAN.getKey().equals(versionType)) {
                strategyCount++;
            } else if (VersionTypeEunm.GETPRICE.getKey().equals(versionType)) {
                getPriceCount++;
            } else if (VersionTypeEunm.DYNAMIC.getKey().equals(versionType)) {
                dynamicCount++;
            }
        }
        //计算调整前的货值
        ValueReviewVersionVO adjustBefore = getAdjustResult(projectId, projectfIds, CollectTypeEnum.ADJUST_BEFORE.getKey());
        //计算调整后货值
        ValueReviewVersionVO adjustAfter = getAdjustResult(projectId, projectfIds, CollectTypeEnum.ADJUST_AFTER.getKey());

        resultList.add(adjustBefore);
        resultList.add(adjustAfter);
        //判断全盘货值概览需要展示的版本
        //1.全投资
        if (approvedVersions.size() == castDecisionCount) {
            int castDecisionNum = 0;
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())) {
                    //审批通过或系统生成的投资版计数
                    if (isValidVersionStage(versionTypeCountDto)) {
                        castDecisionNum++;
                    }
                }
            }
            //统计的投资版个数与分期的个数是否相同
            if (castDecisionNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.INVESTMENT.getKey(), projectPatner);
                resultList.add(maps);
            }
            return resultList;
        }
        //2.全战规/（投资/战规）
        if (approvedVersions.size() == strategyCount || (castDecisionCount + strategyCount) == approvedVersions.size()) {
            int castDecisionNum = 0;
            int strategyNum = 0;
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        castDecisionNum++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())) {
                    //是否对比战规版
                    if (isValidVersionStage(versionTypeCountDto)) {
                        strategyNum++;
                    }
                }
            }

            if (castDecisionNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.INVESTMENT.getKey(), projectPatner);
                resultList.add(maps);
            }

            if (strategyNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.WARPLAN.getKey(), projectPatner);
                resultList.add(maps);
            }
            return resultList;
        }
        //3.全大定价/（投资/大定价）/（战规/大定价）/（投资/战规/大定价）
        if (approvedVersions.size() == getPriceCount || (castDecisionCount + getPriceCount) == approvedVersions.size()
                || (strategyCount + getPriceCount) == approvedVersions.size() ||
                (castDecisionCount + strategyCount + getPriceCount) == approvedVersions.size()) {
            int castDecisionNum = 0;
            int strategyNum = 0;
            int getPriceNum = 0;
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        castDecisionNum++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        strategyNum++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.GETPRICE.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        getPriceNum++;
                    }
                }
            }

            if (castDecisionNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.INVESTMENT.getKey(), projectPatner);
                resultList.add(maps);
            }
            if (strategyNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.WARPLAN.getKey(), projectPatner);
                resultList.add(maps);
            }
            if (getPriceNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.GETPRICE.getKey(), projectPatner);
                resultList.add(maps);
            }
            return resultList;
        }
        //4.全动态/(投资/动态)/(战规/动态)/(大定价/动态)/(投资/战规/动态)/(投资/大定价/动态)/(战规/大定价/动态)/(投资/战规/大定价/动态)
        if (approvedVersions.size() == dynamicCount || (castDecisionCount + dynamicCount) == approvedVersions.size()
                || (strategyCount + dynamicCount) == approvedVersions.size() || (getPriceCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + strategyCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + getPriceCount + dynamicCount) == approvedVersions.size()
                || (strategyCount + getPriceCount + dynamicCount) == approvedVersions.size()
                || (castDecisionCount + strategyCount + getPriceCount + dynamicCount) == approvedVersions.size()
        ) {
            int castDecisionNum = 0;
            int strategyNum = 0;
            int getPriceNum = 0;
            for (VersionTypeCountDto versionTypeCountDto : versionTypeCountDtoList) {
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.INVESTMENT.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        castDecisionNum++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.WARPLAN.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        strategyNum++;
                    }
                }
                if (versionTypeCountDto.getVersionType().equals(VersionTypeEunm.GETPRICE.getKey())) {
                    if (isValidVersionStage(versionTypeCountDto)) {
                        getPriceNum++;
                    }
                }
            }

            if (castDecisionNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.INVESTMENT.getKey(), projectPatner);
                resultList.add(maps);
            }
            if (strategyNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.WARPLAN.getKey(), projectPatner);
                resultList.add(maps);
            }
            if (getPriceNum == stageCount) {
                ValueReviewVersionVO maps = prepareVersionData(projectId, VersionTypeEunm.GETPRICE.getKey(), projectPatner);
                resultList.add(maps);
            }
            return resultList;
        }
        return null;
    }

    private ValueReviewVersionVO getAdjustResult(String projectId, List<String> projectfIds, String type) {
        //版本对象（封装版本和对应的数据）
        ValueReviewVersionVO versionVO = new ValueReviewVersionVO();
        //详情对象（用于封装数据）
        ValueReviewVersionContentVO content = new ValueReviewVersionContentVO();
        //计算返回的数值
        //定义总房源数
        int roomNumCount = 0;
        //定义可售车位数
        int parkingNumCount = 0;
        //定义未推售车位数
        int forSaleParkingNumCount = 0;
        //定义房源货值
        BigDecimal houseCommodityValueCount = BigDecimal.ZERO;
        //定义可售车位货值
        BigDecimal parkingValueCount = BigDecimal.ZERO;
        //定义未推售车位货值
        BigDecimal forSaleParkingValueCount = BigDecimal.ZERO;
        //定义总面积
        BigDecimal saleAreaCount = BigDecimal.ZERO;
        //总货值
        BigDecimal totalValue = BigDecimal.ZERO;
        //权益货值
        BigDecimal quityValue = BigDecimal.ZERO;
        //房源均价
        BigDecimal houseAvg = BigDecimal.ZERO;
        //可售车位均价
        BigDecimal parkingAvg = BigDecimal.ZERO;
        //未推售车位均价
        BigDecimal forSaleParkingAvg = BigDecimal.ZERO;
        //1.查询分期id
        if (CollectionUtils.isNotEmpty(projectfIds)) {
            wc:
            for (String projectfId : projectfIds) {

                //如果分期已被禁用，则不展示该分期
                if (submitApprovedProjectService.stageStateInfo(projectfId)) continue wc;

                List<Map<String, Object>> dynamicValue = submitApprovedProjectService.selectAdjustBefore(projectfId, type);
                if (CollectionUtils.isNotEmpty(dynamicValue)) {
                    for (Map<String, Object> objectMap : dynamicValue) {
                        if (String.valueOf(objectMap.get("type")).equals(OrgTypeEunm.ORG_STAGE.getKey())) {
                            String valueString = priceVersionService.selectArea(objectMap.get("versionId") + "", objectMap.get("version") + "");

                            if (null != objectMap.get("roomNum")) {
                                roomNumCount = roomNumCount + Integer.parseInt(String.valueOf(objectMap.get("roomNum")));
                            }
                            if (null != objectMap.get("parkingNum")) {
                                parkingNumCount = parkingNumCount + Integer.parseInt(String.valueOf(objectMap.get("parkingNum")));
                            }
                            if (null != objectMap.get("forSaleParkingNum")) {
                                forSaleParkingNumCount = forSaleParkingNumCount + Integer.parseInt(String.valueOf(objectMap.get("forSaleParkingNum")));
                            }
                            if (null != objectMap.get("sumHouse")) {
                                houseCommodityValueCount = houseCommodityValueCount.add(new BigDecimal(String.valueOf(objectMap.get("sumHouse"))));
                            }
                            if (null != objectMap.get("sumParkingSaling")) {
                                parkingValueCount = parkingValueCount.add(new BigDecimal(String.valueOf(objectMap.get("sumParkingSaling"))));
                            }
                            if (null != objectMap.get("sumParkingForsale")) {
                                forSaleParkingValueCount = forSaleParkingValueCount.add(new BigDecimal(String.valueOf(objectMap.get("sumParkingForsale"))));
                            }

                            BigDecimal saleArea = StrUtils.isEmpty(valueString) ? BigDecimal.ZERO : new BigDecimal(valueString);
                            saleAreaCount = saleAreaCount.add(saleArea);
                        }
                    }
                }
            }
            //总货值
            totalValue = houseCommodityValueCount.add(parkingValueCount);
            //查询权益比例
            MdmProjectPatner projectPatner = projectBasicService.selecteQuityRatio(projectId);
            if (null != projectPatner.getEquityRatio()) {
                //计算权益货值
                BigDecimal divide = projectPatner.getEquityRatio().divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);
                quityValue = totalValue.multiply(divide);
            }
            //房源均价
            if (null != saleAreaCount && saleAreaCount.compareTo(BigDecimal.ZERO) != 0) {
                houseAvg = houseCommodityValueCount.divide(saleAreaCount, 4, BigDecimal.ROUND_HALF_UP);
            }
            //可售车位均价
            BigDecimal parkingNumCountDec = new BigDecimal(parkingNumCount);
            if (null != Integer.valueOf(parkingNumCount) && parkingNumCountDec.compareTo(BigDecimal.ZERO) != 0) {
                parkingAvg = parkingValueCount.divide(parkingNumCountDec, 4, BigDecimal.ROUND_HALF_UP);
            }
            //未推售车位均价
            BigDecimal forSaleParkingNumCountDec = new BigDecimal(forSaleParkingNumCount);
            if (null != forSaleParkingNumCountDec && forSaleParkingNumCountDec.compareTo(BigDecimal.ZERO) != 0) {
                forSaleParkingAvg = forSaleParkingValueCount.divide(forSaleParkingNumCountDec, 4, BigDecimal.ROUND_HALF_UP);
            }
        }

        content.setRoomNumCount(roomNumCount);
        content.setParkingNumCount(parkingNumCount);
        content.setForSaleParkingNumCount(forSaleParkingNumCount);
        content.setHouseCommodityValueCount(houseCommodityValueCount);
        content.setParkingValueCount(parkingValueCount);
        content.setForSaleParkingValueCount(forSaleParkingValueCount);
        content.setSaleAreaCount(saleAreaCount);
        content.setTotalValue(totalValue);
        content.setQuityValue(quityValue);
        content.setHouseAvg(houseAvg);
        content.setParkingAvg(parkingAvg);
        content.setForSaleParkingAvg(forSaleParkingAvg);

        String typeName = null;
        if (type.equals(CollectTypeEnum.ADJUST_BEFORE.getKey())) {
            typeName = CollectTypeEnum.ADJUST_BEFORE.getValue();
        } else {
            typeName = CollectTypeEnum.ADJUST_AFTER.getValue();
        }
        versionVO.setVersionName(typeName);
        versionVO.setContentVO(content);
        return versionVO;
    }

    private BigDecimal parpareDiffPer(BigDecimal compareValue, BigDecimal afterValue) {
        if (compareValue.compareTo(BigDecimal.ZERO) != 0 && afterValue.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal totalValueDiff = afterValue.subtract(compareValue);
            return totalValueDiff.divide(compareValue, 4, BigDecimal.ROUND_HALF_UP);
        } else if (compareValue.compareTo(BigDecimal.ZERO) == 0 && afterValue.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal totalValueDiff = afterValue.subtract(compareValue);
            return totalValueDiff.divide(afterValue, 4, BigDecimal.ROUND_HALF_UP);
        } else if (compareValue.compareTo(BigDecimal.ZERO) != 0 && afterValue.compareTo(BigDecimal.ZERO) == 0) {
            BigDecimal totalValueDiff = afterValue.subtract(compareValue);
            return totalValueDiff.divide(compareValue, 4, BigDecimal.ROUND_HALF_UP);
        } else {
            return BigDecimal.ZERO;
        }
    }

    private boolean isValidVersionStage(VersionTypeCountDto versionTypeCountDto) {
        return versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SYSGENERATION.getKey()) ||
                versionTypeCountDto.getVersionState().equals(VersionStatusEnum.SUCCESSAPPROVED.getKey());
    }

    private ValueReviewVersionVO prepareVersionData(String projectId, String type, MdmProjectPatner projectPatner) {
        //版本对象（封装版本和对应的数据）
        ValueReviewVersionVO versionVO = new ValueReviewVersionVO();
        //详情对象（用于封装数据）
        ValueReviewVersionContentVO content = new ValueReviewVersionContentVO();
        //转换versionName为中文
        String typeName = null;
        if (type.equals(CollectTypeEnum.CASTDECISION.getKey())) {
            typeName = CollectTypeEnum.CASTDECISION.getValue();
        } else if (type.equals(CollectTypeEnum.WARPLAN.getKey())) {
            typeName = CollectTypeEnum.WARPLAN.getValue();
        } else {
            typeName = CollectTypeEnum.GETPRICE.getValue();
        }
        //计算返回的数值
        //定义总房源数
        int roomNumCount = 0;
        //定义可售车位数
        int parkingNumCount = 0;
        //定义未推售车位数
        int forSaleParkingNumCount = 0;
        //定义房源货值
        BigDecimal houseCommodityValueCount = BigDecimal.ZERO;
        //定义未推售车位货值
        BigDecimal forSaleParkingValueCount = BigDecimal.ZERO;
        //定义可售车位货值
        BigDecimal parkingValueCount = BigDecimal.ZERO;
        //定义总面积
        BigDecimal saleAreaCount = BigDecimal.ZERO;
        //总货值
        BigDecimal totalValue = BigDecimal.ZERO;
        //权益货值
        BigDecimal quityValue = BigDecimal.ZERO;
        //房源均价
        BigDecimal houseAvg = BigDecimal.ZERO;
        //可售车位均价
        BigDecimal parkingAvg = BigDecimal.ZERO;
        //未推售车位均价
        BigDecimal forSaleParkingAvg = BigDecimal.ZERO;

        List<PriceValueOrg> priceValueOrgList = submitApprovedProjectService.selectPriceValueOrgList(projectId, type);
        if (CollectionUtils.isNotEmpty(priceValueOrgList)) {
            for (PriceValueOrg priceValueOrgs : priceValueOrgList) {
                String valueString = priceVersionService.selectArea(priceValueOrgs.getVersionId(), type);
                //总房源数
                Integer roomNum = priceValueOrgs.getRoomNum();
                if (null != roomNum) {
                    roomNumCount = roomNumCount + roomNum;
                } else {
                    roomNumCount = roomNumCount + 0;
                }
                //可售车位数
                Integer parkingNum = priceValueOrgs.getParkingNum();
                if (null != parkingNum) {
                    parkingNumCount = parkingNumCount + parkingNum;
                } else {
                    parkingNumCount = parkingNumCount + 0;
                }
                //未推售车位数
                Integer forsaleParkingNum = priceValueOrgs.getForsaleParkingNum();
                if (null != forsaleParkingNum) {
                    forSaleParkingNumCount = forSaleParkingNumCount + forsaleParkingNum;
                } else {
                    forSaleParkingNumCount = forSaleParkingNumCount + 0;
                }
                //房源货值
                BigDecimal houseCommodityValue = priceValueOrgs.getHouseCommodityValue();
                if (null != houseCommodityValue) {
                    houseCommodityValueCount = houseCommodityValueCount.add(houseCommodityValue);
                } else {
                    houseCommodityValueCount = houseCommodityValueCount.add(BigDecimal.ZERO);
                }
                //未推售车位货值
                BigDecimal commodityValue = priceValueOrgs.getCommodityValue();
                if (null != commodityValue) {
                    forSaleParkingValueCount = forSaleParkingValueCount.add(commodityValue);
                } else {
                    forSaleParkingValueCount = forSaleParkingValueCount.add(BigDecimal.ZERO);
                }
                //可售车位货值
                BigDecimal notCommodityValue = priceValueOrgs.getNotCommodityValue();
                if (null != notCommodityValue) {
                    parkingValueCount = parkingValueCount.add(notCommodityValue);
                } else {
                    parkingValueCount = parkingValueCount.add(BigDecimal.ZERO);
                }
                //总面积

                BigDecimal saleArea = StrUtils.isEmpty(valueString) ? BigDecimal.ZERO : new BigDecimal(valueString);
                saleAreaCount = saleAreaCount.add(saleArea);
            }
            //总货值
            totalValue = houseCommodityValueCount.add(parkingValueCount);
            if (null != projectPatner.getEquityRatio()) {
                //计算权益货值
                BigDecimal divide = projectPatner.getEquityRatio().divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);
                quityValue = totalValue.multiply(divide);
            }
            //房源均价
            if (null != saleAreaCount && saleAreaCount.compareTo(BigDecimal.ZERO) != 0) {
                houseAvg = houseCommodityValueCount.divide(saleAreaCount, 4, BigDecimal.ROUND_HALF_UP);
            }
            //可售车位均价
            BigDecimal parkingNumCountDec = new BigDecimal(parkingNumCount);
            if (null != parkingNumCountDec && parkingNumCountDec.compareTo(BigDecimal.ZERO) != 0) {
                parkingAvg = parkingValueCount.divide(parkingNumCountDec, 4, BigDecimal.ROUND_HALF_UP);
            }
            //未推售车位均价
            BigDecimal forSaleParkingNumCountDec = new BigDecimal(forSaleParkingNumCount);
            if (null != forSaleParkingNumCountDec && forSaleParkingNumCountDec.compareTo(BigDecimal.ZERO) != 0) {
                forSaleParkingAvg = forSaleParkingValueCount.divide(forSaleParkingNumCountDec, 4, BigDecimal.ROUND_HALF_UP);
            }
        }
        content.setRoomNumCount(roomNumCount);
        content.setParkingNumCount(parkingNumCount);
        content.setForSaleParkingNumCount(forSaleParkingNumCount);
        content.setHouseCommodityValueCount(houseCommodityValueCount);
        content.setParkingValueCount(parkingValueCount);
        content.setForSaleParkingValueCount(forSaleParkingValueCount);
        content.setSaleAreaCount(saleAreaCount);
        content.setTotalValue(totalValue);
        content.setQuityValue(quityValue);
        content.setHouseAvg(houseAvg);
        content.setParkingAvg(parkingAvg);
        content.setForSaleParkingAvg(forSaleParkingAvg);

        versionVO.setVersionName(typeName);
        versionVO.setContentVO(content);
        return versionVO;
    }


}
